//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    array: [],
    index: '',
    name: '',
    bankCard: '',
    bankArray: [],
    cardlist:[],
    bank_id:[],
    bank_branch:''
  },
  onLoad() {
    this.cardList();
    this.getUser()
  },
  onShow() {
    var that = this;
    var token = wx.getStorageSync('token');
    wx.request({ //获取银行列表
      url: 'https://www.zbq888.cn/api/v1/bank/list',
      method: 'post',
      data: {},
      header: {
        'content-type': 'application/json', // 默认值
        "client": 1,
        'token': token
      },
      success: res => {
        // console.log(res)
        if (res.data.mark == 502) {
          wx.showToast({
            title: '请先实名认证',
            icon: 'none'
          })
          setTimeout(() => {
            wx.reLaunch({
              url: '../authentication/authentication',
            })
          }, 1000)
        } else {
          that.setData({
            array: res.data.list
          })
        }

      }
    })
  },
  // 填写银行卡号
  getCardnum(e) {
    var cards = e.detail.value;
    var card = cards.replace(/\s/g, '').replace(/(\d{4})(?=\d)/g, "$1 ");
    this.setData({
      bankCard: card
    })
  },
  // 银行支行
  getbankBranch(e){
    var bank_branch = e.detail.value;
    this.setData({
      bank_branch: bank_branch
    })
  },
  // 银行类型列表
  cardList() {
    var that = this;
    var token = wx.getStorageSync('token')
    wx.request({
      url: 'https://www.zbq888.cn/api/v1/bank/list',
      method: 'post',
      data: {},
      header: {
        'content-type': 'application/json', // 默认值
        "client": 1,
        'token': token
      },
      success(res) {
       // console.log("res",res.data)
        if (res.data.mark == 500) {
          wx.showToast({
            title: '您需要登录',
            icon: 'none',
            duration: 1000
          })
          setTimeout(() => {
            wx.reLaunch({
              url: '../login/login'
            })
          }, 1000)
        }
        var cardlist = [...res.data.list]; //放到一个数组里面
        that.setData({
          cardlist :cardlist,
        })
      }
    })
  },
  // picker 确认银行卡类型
  bindPickerChange (e) {
    let index = e.detail.value;
    this.setData({
      index:index
    })
  },
   //实名认证回显
   getUser() {
    var that = this;
    var token = wx.getStorageSync('token');
    wx.request({
      url: 'https://www.zbq888.cn/api/v1/user/real/namelist',
      method: 'post',
      data: {},
      header: {
        'content-type': 'application/json', // 默认值
        "client": 1,
        "token": token
      },
      success: res => {
        that.setData({
          name: res.data.real_name
        })
      }
      
    })
    
  },
  addBank() {
    var that = this;
    var token = wx.getStorageSync('token');
    wx.request({ //添加银行卡
      url: 'https://www.zbq888.cn/api/v1/user/bank/add',
      method: 'post',
      data: {
        "bank_branch":that.data.bank_branch,
        "bank_card_num": that.data.bankCard,
        "bank_id":that.data.cardlist[that.data.index].bank_id
      },
      header: {
        'content-type': 'application/json', // 默认值
        "client": 1,
        'token': token
      },
      success: res => {
        // console.log(res)
        var cardNum = that.data.bankCard;
        if (cardNum == '') {
          wx.showToast({
            title: '请输入银行卡号',
            icon: "none"
          })
        } else if(that.data.bank_branch == '' || null){
          wx.showToast({
            title: '请输入支行',
            icon: 'none'
          })
        } else if (res.data.mark == 1) {
          wx.showToast({
            title: res.data.tip,
            icon: "none"
          })
        } else if(res.data.mark == 0){
          wx.showToast({
            title: res.data.tip,
            icon: "ok"
          })
          setTimeout(() => {
            wx.navigateBack({
              url: '../bank/bank'
            })
          }, 1000)
        }
      }
    })
    // wx.request({ //编辑银行卡
    //   url: 'https://www.zbq888.cn/api/v1/user/bank/update',
    //   method: 'post',
    //   data: {
    //     "bank_card_num": "string",
    //     "bank_id": 0,
    //     "user_bank_id": 0
    //   },
    //   header: {
    //     'content-type': 'application/json', // 默认值
    //     "client": 1,
    //     'token': token
    //   },
    //   success: res => {
    //     console.log(res)
    //   }
    // })
  }
})